<?php
namespace frontend\controllers;
use vendor\pay\lib\PayNotify;
use common\models\Order;
use Yii;
use frontend\controllers\base\BaseController;
/**
 * NeedsController implements the CRUD actions for Needs model.
 */
class PayController extends BaseController
{
    public $enableCsrfValidation = false;
    public $payConfig=[
        //MustPay平台公钥
        'plate_public_key'=>'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDO7CQpYHhEonv1g9YjRVGJDaCOu0bXogD7pBLQu2dDvJ8TGROCEw6ArIWgAWEEE1uEShPBa4MpCP4ZMjT5RMj45o0pb0Z8s4k6CpS9D1LFK9msNpsN8PyaRDQC86R6jxAVQMWgfIZ9cxfZR8Ple3GJGjwBfeRnzh75rE1DHCBOcwIDAQAB',
        //商户私钥
        'mer_private_key'=>'MIICXAIBAAKBgQDMZzQy4iu8s+7KN7VymVEgZlTzfwQbCmtAM+v6rj4YBrdm6qJQ
u8xs/JPjEvd+2oBw6y41YmIjKXOT7Dw1h6KOdq0wdcrR5gnq1qWv47Kegh38At2I
UcI2flV3Hhw68tT0I++Hn9Li9t6ZgsmOjJ+oDe6ykcGxl62Ttgd2as57uwIDAQAB
AoGAf52rrF0i+3THkH9JjNwGF5lsTn4tvQg7P+Fq4p7o66090kaJwqgNkLn9Rxdv
aEbZTCa5OeFL7jISE/e4+57MMNEW7BMa0BrIBvmdwwHykfQ9iH34ye+uHFh4GrgB
wDhXbcGpdxqJ1HHd37m04MQDzqmLe1JwEFsWlI9Nv6Ryw+kCQQD73OxDRvZQstLw
dYoDme1PQGz/Hj7km/+aZNWyaQqu4mXzd0c4c9HBxaZ0pFL+YHfW9hWSZfb8JosS
TI62SrinAkEAz8K2sT4Yf/Xvxy0I1yiQ0nesD4ubr05RoJkDLDFenQ5D4Sdhcdhj
cHuZa4b2qk3P3INFrNG6pn7iNmNtFNgyzQJAJOmjAtUyom8i9TT0zkRQAG57ywQe
yZiHZJRksWots6XgtUcE/CQVWOXj2HKA62EuDyYv0S36fcgUV9E6lULsPwJBAIS2
BUsstiuu5q5dBwOhRvkaNo9rh1Qw/BVRCGTHWH+MaDG/nRBCwCpO2Y6FFLVupPNf
6zbcdZT7AfbqBfjFrZUCQBFRJQBbXk7FSVMEwWvhiaeH/4fmC2XCleRK/NsvJ942
u0xz6he+1tcR1CR9rhH90Qc1b7wncOd+sztMmlKghnY=
',
        //测试商户apps_id
        'apps_id'=>'8cda6b585512455a9ffe79b99ee9bf02',
        //测试商户mer_id
        'mer_id' => '17033016380467631',
        //异步回调URL 此地址必须外网可访问
        'notify_url' => 'http://www.daojianchang.com/pay/pay-notify.html',
        //同步回调URL 此地址必须外网可访问
        'return_url'=> 'http://www.daojianchang.com/pay/pay-return.html',
        // 签名方式
        'sign_type' => 'RSA',
        // 字符编码格式 目前支持utf-8
        'input_charset' => 'utf-8',
        //下单地址
        'add_order_url' => 'https://www.mustpay.com.cn/service/order/saveOrder',
        //订单查询地址
        'query_order_url' => 'https://www.mustpay.com.cn/service/order/queryOrder',
        //ca证书路径地址，用于curl中ssl校验
        //请保证cacert.pem文件在当前文件夹目录中
        'cacert' => '/var/www/vendor/pay/cacert.pem',
    
    ];
    public function actionPayReturn(){
        $get=Yii::$app->request->get();
        $flag=false;
        if(isset($get['out_trade_no']) && isset($get['total_fee']) && isset($get['extra'])){
            $order=Order::find()->where('omun='.$get['out_trade_no'])->one();
            if($order){
                if($order->status==1){
                    $flag=true;
                }else{
                    $status=Order::updateAll(['status'=>1],['omun'=>$get['out_trade_no'],'price'=>($get['total_fee']/100),'flag'=>$get['extra']]);
                    if($status>0){
                        $flag=true;
                    }
                }
            }
        }
        return $this->render('return',['flag'=>$flag,'order'=>$order]);
    }
    public function actionPayNotify(){
        $model=new PayNotify($this->payConfig);
        if($model->verifyNotify()){
            //商户订单号
            $out_trade_no = $_POST['out_trade_no'];
    
            //Mustpay交易号 ,也就是预支付ID
            $total_fee = $_POST['total_fee'];
            $extra = $_POST['extra'];
            Order::updateAll(['status'=>1,'paytime'=>time(),'paytype'=>$_POST['pay_name']],['omun'=>$out_trade_no,'price'=>($total_fee/100),'flag'=>$extra]);
            echo "success";		//请不要修改或删除
        }else{
            //验证失败
            echo "fail";
        }
    }
}
